package com.wing.res.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wing.common.utils.JsonResult;
import com.wing.product.model.response.ProductPlanningTreeVo;

import com.wing.res.model.entity.ResProBom;
import com.wing.res.model.request.ResProBomAddForm;
import com.wing.res.service.ResProBomService;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 产品规划
 * @version 1.1.0
 * @Author hz
 * @Date 2024-09-04 11:10
 **/
@Slf4j
@Validated
@Api(value = "产品工程-产品Bom规划")
@RestController("productPlanBomController")
@RequestMapping("res/admin/planning/proBom")
public class ProductPlanBomController {

    @Autowired
    private ResProBomService resProBomService;

    /**
     * 获取产品的Bom树
     *
     */
    @ApiOperation(value = "查询客户产品规划Bom树", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId", value="产品id", defaultValue = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/bomTree/{productId}")
    public JsonResult bomTree(@PathVariable("productId") String productId){
        //拿到productId 查询为-1的节点
        QueryWrapper<ResProBom> info = new QueryWrapper<>();
        info.eq("product_detail_id",productId);
        info.eq("parent_id",-1L);
        ResProBom one = resProBomService.getOne(info);
        Long id = null;
        if (ObjectUtil.isNotEmpty(one)) {
            id = one.getId();
        }
        List<ProductPlanningTreeVo> vos = resProBomService.selectBomTreeBySkuId(id);
        return JsonResult.success(vos);
    }


    @ApiOperation(value = "添加产品-bom", notes = "添加-bom")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/addBom")
    public JsonResult addBom(@RequestBody ResProBomAddForm resProBomAddForm) {
        if (resProBomService.addBom(resProBomAddForm) > 0) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }


    @ApiOperation(value = "修改-bom", notes = "修改-bom")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/updateBom")
    public JsonResult updateBom(String id,
                                Integer count,String remark) {
        if (resProBomService.updateBom(id,count,remark) > 0) {
            return JsonResult.success("修改成功");
        }
        return JsonResult.fail("修改失败");
    }

    @ApiOperation(value = "删除-bom", notes = "删除-bom")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @DeleteMapping("/deleteBom/{id}")
    public JsonResult deleteBom(@PathVariable("id") String id) {
        if (resProBomService.deleteBom(id) > 0) {
            return JsonResult.success("操作成功");
        }
        return JsonResult.fail("操作失败");
    }
}
